package org.biubiu0002.baiblog.core.controller;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.biubiu0002.baiblog.common.utils.R;
import org.biubiu0002.baiblog.core.annotations.PermissionIgnore;
import org.biubiu0002.baiblog.core.model.dto.TokenUserDto;
import org.biubiu0002.baiblog.core.model.entity.MessageEntity;
import org.biubiu0002.baiblog.core.service.MessageService;
import org.biubiu0002.baiblog.core.utils.SecurityUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: biubiu0002
 * @Date: 2022/10/25 19:54
 **/
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Resource
    private MessageService messageService;

    @GetMapping("/historyMsg")
    @PermissionIgnore
    public R historyMsg(Long lastMsgId, String toChatUid, @RequestParam(value = "type") Integer type){
        Preconditions.checkArgument(StringUtils.isNotBlank(toChatUid));
        Preconditions.checkArgument(type==1||type==2||type==3,"type只能为1或2或3");
        TokenUserDto user = SecurityUtils.getUser();
        String userUid = user.getUserUid();
        Preconditions.checkArgument(!StringUtils.equals(userUid,toChatUid), "两个用户id不能一致");
        List<MessageEntity> historyMsg = messageService.getHistoryMsg(lastMsgId,userUid,toChatUid,type);
        return R.ok().put("data",historyMsg);
    }


}
